import sympy as sp

# 定义符号变量
x = sp.symbols('x')

# 符号表达式求值
expr = x**2 + 2*x + 1
result = expr.subs(x, 5)  # 代入x=5
print("代入求值:", result) # 代入求值: 36

# 高精度计算
pi_val = sp.N(sp.pi, 50)  # 计算π的50位精度值
print("π的50位精度:", pi_val) # π的50位精度: 3.1415926535897932384626433832795028841971693993751

# 转换为数值函数
expr = x**2 + sp.sin(x)
f = sp.lambdify(x, expr, 'numpy')  # 转换为NumPy函数

import numpy as np
x_vals = np.linspace(0, 10, 100)
y_vals = f(x_vals)
print("数值计算完成，数据点数:", len(y_vals)) # 数值计算完成，数据点数: 100